﻿// AreaDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "VideoPlayer.h"
#include "AreaDlg.h"
#include "afxdialogex.h"

UINT AreaThreadShowPic(PVOID pParam);
// CAreaDlg 对话框

IMPLEMENT_DYNAMIC(CAreaDlg, CDialogEx)

CAreaDlg::CAreaDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_DLG_AREA, pParent), m_Drawing(false)
{

}

CAreaDlg::~CAreaDlg()
{
}

void CAreaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_AREA_GRID, m_grid);
}


BEGIN_MESSAGE_MAP(CAreaDlg, CDialogEx)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDOK, &CAreaDlg::OnBnClickedOk)
//	ON_WM_RBUTTONDBLCLK()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_32773, &CAreaDlg::On32773)
END_MESSAGE_MAP()


// CAreaDlg 消息处理程序
BOOL CAreaDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	if (GetDlgItem(IDC_AREA_PIC) != NULL)
		GetDlgItem(IDC_AREA_PIC)->GetClientRect(&m_rect);
	int no = g_pLightQueue->getLastFrameNo();
	//队列为空（尚未开机）
	if (no == 0) {
		MessageBox(_T("请打开相机"));
		return TRUE;
	}
	g_pLightQueue->getLast(m_Frame);
	//此处一定要clone，否则在onDisplay中进行的绘制操作，都会被其他模块中getLast时带入
	m_Frame.frame = m_Frame.frame.clone();
	CWinThread* pShowThread = AfxBeginThread(AreaThreadShowPic, this, THREAD_PRIORITY_ABOVE_NORMAL, 0, CREATE_SUSPENDED);
	pShowThread->m_bAutoDelete = FALSE;
	pShowThread->ResumeThread();

	HDITEM item;
	item.cxy = 120;
	item.mask = HDI_WIDTH;
	m_grid.GetHeaderCtrl().SetItem(0, new HDITEM(item));
	CMFCPropertyGridProperty* pProp1 = new CMFCPropertyGridProperty(
		_T("x"),
		_variant_t((short)0, VT_I2),
		_T("左上点坐标X"));
	CMFCPropertyGridProperty* pProp2 = new CMFCPropertyGridProperty(
		_T("y"),
		_variant_t((short)0, VT_I2),
		_T("左上点坐标Y"));
	CMFCPropertyGridProperty* pProp3 = new CMFCPropertyGridProperty(
		_T("w"),
		_variant_t((short)0, VT_I2),
		_T("框宽"));
	CMFCPropertyGridProperty* pProp4 = new CMFCPropertyGridProperty(
		_T("h"),
		_variant_t((short)0, VT_I2),
		_T("框高"));
	m_grid.AddProperty(pProp1);
	m_grid.AddProperty(pProp2);
	m_grid.AddProperty(pProp3);
	m_grid.AddProperty(pProp4);
	for (int i = 0; i < g_pMainDlg->m_areas.size(); i++) {
		m_boxes.push_back(g_pMainDlg->m_areas[i]);
	}
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}
/*******************************************************************************
* 可见光显示线程
* @yuv      待显示帧
* @frameNo  待显示帧帧号
******************************************************************************/
UINT AreaThreadShowPic(PVOID pParam) {
	Sleep(500);
	CAreaDlg* pThis = (CAreaDlg*)pParam;
	pThis->OnDisplay();
	return 0;
}

void CAreaDlg::getRelativePos(CPoint& p) {
	CRect rect;
	GetDlgItem(IDC_AREA_PIC)->GetWindowRect(&rect);
	this->ClientToScreen(&p);
	CPoint rectTL = rect.TopLeft();
	LONG rx_1 = p.x - rectTL.x;
	LONG ry_1 = p.y - rectTL.y;
	float wRatio = (float)m_Frame.frame.cols / m_rect.Width();
	float hRatio = (float)m_Frame.frame.rows / m_rect.Height();
	CvPoint tlPoint = cvPoint((int)rx_1*wRatio, (int)ry_1*hRatio);
	p.x = tlPoint.x;
	p.y = tlPoint.y;
}

void CAreaDlg::OnDisplay()  //负责调整大小后调用display显示到picture控件
{
	Mat tempFrame = m_Frame.frame.clone();
	if (m_Drawing) {
		CRect rect;
		GetDlgItem(IDC_AREA_PIC)->GetWindowRect(&rect);
		CPoint oldPoint = m_OldPoint;
		CPoint currentPoint = m_CurrentPoint;
		this->ClientToScreen(&oldPoint);
		this->ClientToScreen(&currentPoint);
		CPoint rectTL = rect.TopLeft();
		LONG rx_1 = oldPoint.x - rectTL.x;
		LONG ry_1 = oldPoint.y - rectTL.y;
		LONG rx_2 = currentPoint.x - rectTL.x;
		LONG ry_2 = currentPoint.y - rectTL.y;
		float wRatio = (float)m_Frame.frame.cols / m_rect.Width();
		float hRatio = (float)m_Frame.frame.rows / m_rect.Height();
		CvPoint tlPoint = cvPoint((int)rx_1*wRatio, (int)ry_1*hRatio);
		CvPoint brPoint = cvPoint((int)rx_2*wRatio, (int)ry_2*hRatio);
		int m_x = tlPoint.x;
		int m_y = tlPoint.y;
		int m_w = brPoint.x - tlPoint.x;
		int m_h = brPoint.y - tlPoint.y;
		m_boxes[m_boxes.size() - 1].x = m_x;
		m_boxes[m_boxes.size() - 1].y = m_y;
		m_boxes[m_boxes.size() - 1].w = m_w;
		m_boxes[m_boxes.size() - 1].h = m_h;
		
		
		CMFCPropertyGridProperty* p = m_grid.GetProperty(0);
		
		p->SetValue(_variant_t(short(m_x), VT_I2));
		p = m_grid.GetProperty(1);
		p->SetValue(_variant_t(short(m_y), VT_I2));
		p = m_grid.GetProperty(2);
		p->SetValue(_variant_t(short(m_w), VT_I2));
		p = m_grid.GetProperty(3);
		p->SetValue(_variant_t(short(m_h), VT_I2));
	}
	for (int i = 0; i < m_boxes.size(); i++) {
		cv::rectangle(tempFrame, cvPoint(m_boxes[i].x, m_boxes[i].y), cvPoint(m_boxes[i].x + m_boxes[i].w, m_boxes[i].y + m_boxes[i].h), Scalar(0, 0, 255), 2, 1, 0);
	}
	IplImage iplImage(tempFrame); //转换成IplImage类型
	CDC* pDC = GetDlgItem(IDC_AREA_PIC)->GetDC();
	CvvImage cvImg;
	cvImg.CopyOf(&iplImage);
	cvImg.DrawToHDC(pDC->m_hDC, &m_rect);
	ReleaseDC(pDC);
	
}

void CAreaDlg::OnLButtonDown(UINT nFlags, CPoint point) {
	m_Drawing = true;
	m_OldPoint = point;
	m_CurrentPoint = point;
	m_boxes.push_back(Box(0, 0, 0, 0));
}

void CAreaDlg::OnMouseMove(UINT nFlags, CPoint point)

{
	if (m_Drawing) {
		m_CurrentPoint = point;
		OnDisplay();
	}
}

void CAreaDlg::OnLButtonUp(UINT nFlags, CPoint point) {

	if (m_Drawing) {
		m_Drawing = false;
		m_CurrentPoint = point;
		//OnDisplay();
	}
}

static CPoint rcPoint; //右点击点的坐标
//点击右键，弹出“删除”菜单
void CAreaDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	int index = -1;
	rcPoint = point;
	getRelativePos(rcPoint);
	for (int i = 0; i < m_boxes.size(); i++) {
		CRect r(m_boxes[i].x, m_boxes[i].y, m_boxes[i].x + m_boxes[i].w, m_boxes[i].y + m_boxes[i].h);
		if (CUtils::inRect(rcPoint, r)) {
			index = i;
			break;
		}
	}
	if (index == -1) {
		return;
	}

	CMenu menu;
	menu.LoadMenu(IDR_MENU1); //装载自定义的右键菜单 
	CMenu *pContextMenu = menu.GetSubMenu(0); //获取第一个弹出菜单，所以第一个菜单必须有子菜单 
	CPoint point1;//定义一个用于确定光标位置的位置  
	GetCursorPos(&point1);//获取当前光标的位置，以便使得菜单可以跟随光标  
	pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point1.x, point1.y, this); //在指定位置显示弹出菜单
	CDialogEx::OnRButtonDown(nFlags, point);
}

//点击删除
void CAreaDlg::On32773()
{
	// TODO: 在此添加命令处理程序代码
	cout << rcPoint.x << rcPoint.y << endl;
	cout << m_boxes[0].x << m_boxes[0].y << m_boxes[0].w << m_boxes[0].h << endl;
	
	int index = -1;
	for (int i = 0; i < m_boxes.size(); i++) {
		CRect r(m_boxes[i].x, m_boxes[i].y, m_boxes[i].x + m_boxes[i].w, m_boxes[i].y + m_boxes[i].h);
		if (CUtils::inRect(rcPoint, r)) {
			index = i;
			break;
		}
	}
	if (index != -1) {
		std::vector<Box>::iterator it = m_boxes.begin() + index;
		m_boxes.erase(it);
		OnDisplay();
	}
}


void CAreaDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	g_pMainDlg->m_areas.clear();
	for (int i = 0; i < m_boxes.size(); i++) {
		g_pMainDlg->m_areas.push_back(m_boxes[i]);
	}
	CDialogEx::OnOK();
}



